# -*- coding: utf-8 -*-
"""
Created on Thu Aug  9 22:46:21 2018

@author: Hy
"""
import multiprocessing
import os


def writetxt(m):
    model = __import__(m)
    s = str(model.main())
    with open('code.txt', 'a') as f:
        f.write(s + '\r\n')


if __name__ == "__main__":
    l = os.listdir()

    pool = multiprocessing.Pool(processes=3)
    for x in l:
        if os.path.splitext(x)[1] == '.py' and os.path.splitext(x)[0] != 'start':
            m = os.path.splitext(x)[0]
            pool.apply(writetxt, (m,))  # 维持执行的进程总数为processes，当一个进程执行完毕后会添加新的进程进去
        print('====={}正在记录====='.format(m))
    pool.close()
    pool.join()  # 调用join之前，先调用close函数，否则会出错。执行完close后不会有新的进程加入到pool,join函数等待所有子进程结束
    print('完成')
